import pyautogui

_leftAxis = 1140
_rightAxis = 1150
_yAxis = 886
_originalColor = (132, 158, 206)
preBarColor = [(0, 0, 0) for _ in range(_leftAxis, _rightAxis)]
preventErrorJudge = [
    # # [(788, 456), (245, 245, 245)],
    # [(1140, 886), (250, 140, 100)],
    # [(1144, 886), (250, 140, 100)]
]
r = (123, 48, 57)
g = (90, 97, 49)
b = (66, 77, 165)
left = [  # 左1-左4
    [(716, 701), (739, 704), (774, 684), (776, 670)],
    [(515, 705), (525, 704), (559, 677), (563, 685)],
    [(304, 644), (306, 704), (352, 681), (337, 695)],
    [(58, 626), (91, 704), (108, 701), (135, 587)]
]
right = [  # 右1-右4
    [(1105, 677), (1133, 704), (1155, 703), (1144, 703)],
    [(1315, 672), (1323, 692), (1344, 704), (1351, 708)],
    [(1530, 671), (1539, 691), (1560, 704), (1593, 698)],
    [(1741, 675), (1767, 704), (1752, 694), (1803, 701)]
]
colorOfSnakes = ['', '绿色', '蓝色', '红色']
stopCheck = [
    [(126, 725), (115, 105, 107)],
    [(159, 745), (115, 105, 107)]
]


def start():
    # print(preBarColor)
    cc = True
    for _colorPre in preBarColor:
        cc = cc and (_colorPre != _originalColor)
    preBarColor.clear()
    _img = pyautogui.screenshot()
    for _ in range(_leftAxis, _rightAxis):
        preBarColor.append(_img.getpixel((_, _yAxis)))
    if cc:
        for _ in preBarColor:
            cc = cc and (_ == _originalColor)
    pyautogui.sleep(1)
    return cc


def check_color_eq(_color1: tuple, _color2: tuple) -> bool:
    eps = 5
    for i in range(3):
        if abs(_color1[i] - _color2[i]) > eps:
            return False
    return True
    # return _color1[0] > 230 and _color1[1] < 200 and _color1[2] < 110


def check_color_is_fever(color1: tuple) -> bool:
    return color1[0] > 252


def check_color_error(_color1: tuple, _color2: tuple) -> bool:
    print(_color1, _color2)
    return _color1[0] > _color2[0] and _color1[1] < _color2[1] and _color1[2] < _color2[2]


def is_fever(_img) -> bool:
    cc = True
    preBarColor.clear()
    for _ in range(_leftAxis, _rightAxis):
        preBarColor.append(_img.getpixel((_, _yAxis)))
    print(preBarColor)
    if cc:
        cnt = 0
        for _ in preBarColor:
            # cc = cc and (not check_color_eq(_, _originalColor))
            if check_color_is_fever(_):
                cnt += 1
        if cnt / (_rightAxis - _leftAxis) < 0.8:
            cc = False
        print('cnt=', cnt)
    for errorJudge in preventErrorJudge:
        cc = cc and check_color_error(_img.getpixel(errorJudge[0]), errorJudge[1])
    return cc


def fever():
    global preBarColor
    print('--------------------------------------------')
    print('fever')
    pyautogui.press('A', 500, 0.01, _pause=False)
    preBarColor = [_originalColor for _ in range(_leftAxis, _rightAxis)]
    pyautogui.sleep(1.002)
    # pyautogui.press('A', 3, 0.1, _pause=False)


def click_left(num: int):
    x = 224
    y = 999
    for _i in range(num):
        pyautogui.click(x=x+_i, y=y+_i, button='left', _pause=False)
        pyautogui.sleep(0.06)

    # pyautogui.press('1', num, 0.1, _pause=False)
    print('click left', num)


def click_right(num: int):
    x = 1547
    y = 940
    for _i in range(num):
        pyautogui.click(x=x+_i, y=y+_i, button='left', _pause=False)
        pyautogui.sleep(0.06)
    # pyautogui.press('2', num, 0.1, _pause=False)
    print('click right', num)


def check_whether_to_click(_img, check_points: list) -> int:
    for point in check_points:
        _color = _img.getpixel(point)
        if _color == r:
            return 3
        elif _color == b:
            return 2
        elif _color == g:
            return 1
    return 0


def is_stop(_img) -> bool:
    for _c in stopCheck:
        if _img.getpixel(_c[0]) != _c[1]:
            return False
    return True


if __name__ == '__main__':
    # screenshots = []
    while not start():
        pass

    print('game start')
    pyautogui.sleep(2)

    while True:
        img = pyautogui.screenshot()
        if is_stop(img):
            break
        direction = []
        printDivide = False
        for i in range(0, 4):
            clickNumOfLeft = check_whether_to_click(img, left[i])
            clickNumOfRight = check_whether_to_click(img, right[i])

            if clickNumOfLeft != 0:
                print('左边', colorOfSnakes[clickNumOfLeft])
                printDivide = True
                direction.append([click_left, clickNumOfLeft])
            if clickNumOfRight != 0:
                print('右边', colorOfSnakes[clickNumOfRight])
                printDivide = True
                direction.append([click_right, clickNumOfRight])
        for func, param in direction:
            func(param)
            if is_fever(img):
                fever()
                printDivide = False
                print('--------------------------------------------')
                break
        if printDivide:
            # screenshots.append(img)
            print('--------------------------------------------')
            pyautogui.sleep(0.33)


